﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Generic;


namespace TravelAgencySilverlight.ViewModels
{
    public class ViewModelTrips : ViewModelBase
    {
        private ObservableCollection<ViewModelPhoto> photos;
        public ObservableCollection<ViewModelPhoto> Photos
        {
            get
            {
                if (this.photos == null)
                {
                    this.photos = new ObservableCollection<ViewModelPhoto>();
                    this.ReloadPhotosAsync();
                }

                return this.photos;
            }
        }

        private void ReloadPhotosAsync()
        {
            this.Photos.Clear();

            Models.TravelAgencyModel.TryGetPhotosAsync((Exception exception, IEnumerable<TravelAgencySilverlight.Web.Photo> photos) =>
            {
                if (exception == null)
                {
                    lock (this.Photos)
                    {
                        this.Photos.Clear();

                        foreach (var photo in photos)
                        {
                            ViewModelPhoto vmPhoto = new ViewModelPhoto(photo);
                            this.Photos.Add(vmPhoto);
                        }
                    }
                }
                else
                {
                    if (exception is InvalidCastException)
                    {
                        MessageBox.Show("cannot load database my ass");
                    }
                    else if (exception is InvalidOperationException)
                    {
                        MessageBox.Show("unauthenticated user");
                    }
                    else
                    {
                        MessageBox.Show("error, cannot load photos");
                    }
                }
            });
        }        
    }
}
